package cn.cqs.redisitem.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.PropertyConfigurator;

import javax.servlet.http.HttpServlet;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@Slf4j
public class LoginitPathServlet  extends HttpServlet{

public void init(){
//    获取servlet上下文
    String path = this.getServletContext().getRealPath("/");
//    servlet参数,获取log4j.property文件路径
    String file=this.getInitParameter("log4j_init_path");
//    自定义文件路径
    String logFile=this.getInitParameter("log4j_file_path");
    if(file !=null){
        //读取配置文件property
        Properties prop = new Properties();
        try {
            //加载log4j.properties文件
            prop.load(new FileInputStream(path+logFile));
            //添加配置信息(设置日志文件的输出路径)
            prop.setProperty("log4j.appender.File", path+logFile+ prop.getProperty("log4j.appender.File"));
            //加载配置项
            PropertyConfigurator.configure(prop);
        } catch (IOException e) {
            log.info("初始化log4j日志输入路径异常，请检查web.xml参数配置是否正常，异常发生在"+this.getClass().getName()+"类的public void init()方法，异常的愿意是："+e.getMessage(), e.fillInStackTrace());
            e.printStackTrace();
        }


    }





}


}
